(function () {
    var myApp = angular.module('myApp');

    //角色列表
    myApp.controller("roleListController", function ($scope, $rootScope, $log, myRoleMenuTableOption, reqDataByAjax) {
        // TAB显示之后，调整表格大小
        $scope.$on("shown.bs.tab", function (event, element, target) {
            if (target.is('#rolelist')) {
                $scope.$root.$broadcast('resize.table', '#roletable');
            }
        });

        $scope.option = myRoleMenuTableOption;
        $scope.option.ajax = ajax;

        function ajax(data, callback, settings) {
            reqDataByAjax("/scheduling/api/user/roleMenu/role", data).then(function (ret) {
                callback({
                    "draw": data.draw,
                    "recordsTotal": ret[1][0].recordsTotal,
                    "recordsFiltered": ret[2][0].recordsFiltered,
                    "data": ret[0]
                });
            });
        }

        $scope.$on("select", function (event, element, e, dt, type, indexes) {
            if (element.is('#roletable')) {
                $scope.$root.selectedRoleMenu = Array.prototype.slice.call(dt.rows('.selected').data(), 0);
                $scope.$root.$broadcast('show.tab', '#menuGroupTab');
                $scope.$root.$broadcast('discloseAll.tree', '#menuGroupTree');
            }
        });

        $scope.$on("deselect", function (event, element, e, dt, type, indexes) {
            if (element.is('#roletable')) {
                $scope.$root.selectedRoleMenu = Array.prototype.slice.call(dt.rows('.selected').data(), 0);
                $scope.$root.$broadcast("show.tab", '#help');
            }
        });

        $scope.$on("draw.dt", function (event, element, e, settings) {
            if (element.is('#roletable')) {
                $scope.$root.selectedRoleMenu = [];
                $scope.$root.$broadcast("show.tab", '#help');
            }
        });

    });

    myApp.controller("menuGroupTabController", function ($scope, $log, reqDataByAjax, $q) {
        // 设置默认系统选项
        $scope.$root.selectedSystemId = 'DM';
        $scope.$root.$watch("selectedSystemId", function (newVal, oldVal, scope) {
            scope.$broadcast('refresh.tree');
        });

        // 设置菜单项树结构的Option
        function dataSource(parentMenuGroup, callback) {
            var params = {
                system_id: $scope.$root.selectedSystemId,
                role_id: $scope.$root.selectedRoleMenu && $scope.$root.selectedRoleMenu.length &&
                $scope.$root.selectedRoleMenu[0].role_id,
                group_id: parentMenuGroup && parentMenuGroup.group_id
            };

            $q.all([reqDataByAjax("/scheduling/api/user/roleMenu/group", params),
                reqDataByAjax("/scheduling/api/user/roleMenu/item", params)])
                .then(function (data) {
                    var ret = [];
                    if (!data.length)
                        return;

                    for (var i = 0; i < data[0][0].length; ++i) {
                        var item = data[0][0][i];
                        item['type'] = 'folder';
                        ret.push(item);
                    }

                    for (var i = 0; i < data[1][0].length; ++i) {
                        var item = data[1][0][i];
                        item['type'] = 'item';
                        item['checked'] && (item.attr = { 'cssClass': 'tree-selected', 'data-icon': 'glyphicon glyphicon-ok' });
                        ret.push(item);
                    }

                    callback({ data: ret });
                });
        }

        $scope.option = {
            dataSource: dataSource,
            cacheItems: false,
            multiSelect: true,
            folderSelect: false
        };

        // 调用服务新建菜单组
        var url = "/scheduling/api/user/roleMenu/saveRoleMenu";
        $scope.ok = function () {
            var selectedItems = $('#menuGroupTree').tree('selectedItems');
            for (var i in selectedItems)
                selectedItems[i].role_id = $scope.$root.selectedRoleMenu &&
                    $scope.$root.selectedRoleMenu.length && $scope.$root.selectedRoleMenu[0].role_id;

            reqDataByAjax(url, selectedItems, 'POST').then(function () {
                // 显示保存成功对话框
            });
        };

    });


})();


